﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_F4Export : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        public panel_F4Export()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.F4ExportProduct.DoubleClick += F4ExportProduct_DoubleClick;
            this.F4ExportProduct.PopupMenuShowing += F4ExportProduct_PopupMenuShowing;
            this.f4ExportProductContextMenu.ItemClicked += f4ExportProductContextMenu_ItemClicked;
        }

        void f4ExportProductContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.f4ExportProductContextMenu.Hide();
            DataRow row = F4ExportProduct.GetFocusedDataRow();
            switch (e.ClickedItem.Name)
            {                
                case "addSelection":
                    frm_F4Export export_Form = new frm_F4Export();
                    export_Form.ShowDialog();
                    LoadData();
                    break;                
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void F4ExportProduct_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.f4ExportProductContextMenu.Show(MousePosition);
        }

        private void F4ExportProduct_DoubleClick(object sender, EventArgs e)
        {   
            /*
            DataRow row = F4ExportProduct.GetFocusedDataRow();
            frm_Edit_F4ExportTroubles editForm = new frm_Edit_F4ExportTroubles();
            editForm.troubleID = Int32.Parse(row[0].ToString());
            if (editForm.LoadData())
            {
                editForm.Show();
            } */                        
        }
        
        //////////////////////////////////////////////////
        /// Load data from database to grid
        //////////////////////////////////////////////////
        private void LoadData()
        {
            bool have_data = true;
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT [231_RobotOrderDetail].SerialNo, [231_RobotOrderDetail].CompID,"
                + " [231_RobotOrderDetail].OrderNo, [231_RobotOrderDetail].PlanNo, [231_RobotOrderDetail].Exported,"
                + " [231_RobotOrderDetail].ExportedDate, [231_RobotOrderDetail].Finished, [222_Customer].FullName,"
                + " [231_RobotOrderDetail].RobotSerialNo, [231_RobotOrderDetail].Remark"
                + " FROM [231_RobotOrderDetail] LEFT OUTER JOIN"
                + " [222_Customer] ON [222_Customer].CustomerID=[231_RobotOrderDetail].CustomerID"
                + " WHERE ([231_RobotOrderDetail].Exported = 0) AND ([231_RobotOrderDetail].Finished = 1)"
                + " ORDER BY [231_RobotOrderDetail].SerialNo";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridF4ExportProduct.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("SerialNo", typeof(string));
                newTable.Columns.Add("CompID", typeof(string));
                newTable.Columns.Add("OrderNo", typeof(string));
                newTable.Columns.Add("PlanNo", typeof(string));
                newTable.Columns.Add("Exported", typeof(string));
                newTable.Columns.Add("ExportedDate", typeof(DateTime));
                newTable.Columns.Add("Finished", typeof(string));
                newTable.Columns.Add("Customer", typeof(string));
                newTable.Columns.Add("RobotSerialNo", typeof(string));
                newTable.Columns.Add("Remark", typeof(string));
                gridF4ExportProduct.DataSource = newTable;
                have_data = false;
            }
            F4ExportProduct.Columns[0].Caption = "Serial No";
            F4ExportProduct.Columns[1].Caption = "Comp ID";
            F4ExportProduct.Columns[2].Caption = "Order No.";
            F4ExportProduct.Columns[3].Caption = "Plan No";            
            F4ExportProduct.Columns[4].Caption = "Exported";
            F4ExportProduct.Columns[5].Caption = "Exported Date";
            F4ExportProduct.Columns[6].Caption = "Finished";
            F4ExportProduct.Columns[7].Caption = "Customer";
            F4ExportProduct.Columns[8].Caption = "Robot Serial No";
            F4ExportProduct.Columns[9].Caption = "Remark";            
            F4ExportProduct.Columns[0].Visible = false;
            F4ExportProduct.BestFitColumns();

            if (!have_data)
            {
                MessageBox.Show("There is no available Comp to Export");
            }
        }

        private void bottonAdd_Click(object sender, EventArgs e)
        {
            frm_F4Export export_Form = new frm_F4Export();
            export_Form.ShowDialog();
            LoadData();
        }

        private void bottonEdit_Click(object sender, EventArgs e)
        {            
            /*
            DataRow row = F4ExportProduct.GetFocusedDataRow();
            frm_Edit_F4ExportTroubles editForm = new frm_Edit_F4ExportTroubles();
            editForm.troubleID = Int32.Parse(row[0].ToString());
            if (editForm.LoadData())
            {
                editForm.Show();
            }  */
        }
    }
}
